<?php
/**
 * model-文章主题管理
 * [ranknow bbs!] (C)2009-2015 ranknow.cn.
 *
 * @author liwei
 * @This is NOT a freeware, use is subject to license terms
 * @rcsfile $RCSfile: mdl_thread,v $
 * @revision $Revision: 0.1 $
 * @date $Date: 2009/12/30 03:40:17 $
 */
function add_thread( $data = '' ) {
    if ( !is_array( $data ) || count( $data ) == 0 ) {
        return false;
    } else {
        $sql = sqlMakeInsert( 'bbs_threads', $data );
        return dbExecute( $sql );
    }
}

function update_thread( $data = '', $tid = 0 ) {
    if ( !is_array( $data ) || count( $data ) == 0 ) {
        return false;
    } else {
        $sql = sqlMakeUpdateById( 'bbs_threads', 'tid', $tid, $data );
        return dbExecute( $sql );
    }
}

function inc_thread_views($tid){
  $sql = "UPDATE bbs_threads SET views=views+1 WHERE tid='$tid'";
  return dbExecute( $sql );
}


function delete_thread_by_id( $id = 0 ) {
    if ( $id > 0 ) {
        $sql = sqlMakeDelById( 'bbs_threads', 'tid', $id );
        return dbExecute( $sql );
    } else {
        return false;
    }
}

function get_thread_list( $offset = 0, $pageSize = 10, $fid = 0 ) {
    $data = array();
    $sql = 'SELECT '
     . ' bbs_threads.tid,'
     . ' bbs_threads.fid,'
     . ' bbs_threads.authorid,'
     . ' bbs_threads.author,'
     . ' bbs_threads.subject,'
     . ' bbs_threads.dateline,'
     . ' bbs_threads.lastpost,'
     . ' bbs_threads.lastposter,'
     . ' bbs_threads.views,'
     . ' bbs_threads.replies,'
     . ' bbs_forums.name AS forum_name'
     . ' FROM'
     . ' bbs_threads'
     . ' LEFT OUTER JOIN bbs_forums ON (bbs_threads.fid = bbs_forums.fid)';
    if ( $fid ) {
        $sql .= " WHERE bbs_threads.fid='$fid'";
    }
    $sql .= 'ORDER BY bbs_threads.lastpost DESC';
    $data = dbPageQuery( $sql, $offset, $pageSize );
    if ( is_array( $data['data'] ) && count( $data['data'] ) > 0 ) {
        foreach( $data['data'] as $key => $item ) {
            $data['data'][$key]['dateline'] = easydate( $item['dateline'] );
            $data['data'][$key]['lastpost'] = easydate( $item['lastpost'] );
        }
    }
    return $data;
}

function get_thread_by_id( $tid = 0 ) {
    $thread = array();
    if ( $tid > 0 ) {
        $sql = "SELECT views,replies,fid,dateline,lastpost FROM bbs_threads WHERE tid='$tid'";
        $thread = dbGetRow( $sql );
        $thread['dateline'] = easydate( $thread['dateline'] );
        $thread['lastpost'] = easydate( $thread['lastpost'] );
    }
    return $thread;
}

?>